/*
 *   Copyright 2023 <a href="mailto:asialjim@hotmail.com">Asial Jim</a>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.asialjim.encommunicate.base.rsa.key;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * RSA密钥长度
 *
 * @author Copyright © <a href="mailto:asialjim@hotmail.com">Asial Jim</a>   Co., LTD
 * @version 1.0
 * @since 2023/2/24, &nbsp;&nbsp; <em>version:1.0</em>, &nbsp;&nbsp; <em>java version:17</em>
 */
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum RSAKeySize {
    LEN_512(1 << 9),
    LEN_1K(1 << 10),
    LEN_2K(1 << 11),
    LEN_3K(1 << 12);
    private final int length;

    public static RSAKeySize sizeOf(Integer size) throws Throwable {
        if (Objects.isNull(size)) {
            throw new IllegalArgumentException("错误的密钥长度配置，可选配置: 512,1024,2048,4096");
        }

        return Arrays.stream(values())
                .filter(item -> item.getLength() == size)
                .findFirst()
                .orElseThrow((Supplier<Throwable>) () -> new IllegalArgumentException("错误的密钥长度配置，可选配置: 512,1024,2048,4096"));
    }
}